﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcBHXH.Models;
using Newtonsoft.Json;
using System.Web.Script.Serialization;
using System.IO;
using System.Text;

namespace MvcBHXH.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewData["Message"] = "Welcome to ASP.NET MVC!";
            return View();
        }

        public ActionResult About()
        {
            return View();
        }

        public object GetMaBHYTTNModel()
        {
            MaBHYTTNModel maBHYTTNModel = new MaBHYTTNModel();
            string json = JsonConvert.SerializeObject(maBHYTTNModel.GetMaBHYTTNModel(), Formatting.Indented);
            JavaScriptSerializer json_serializer = new JavaScriptSerializer();
            return json;
        }

        public object SearchBHYTTNModel(string maThe, string hoTen, string loaiBH, string dotBh, string mucBH)
        {
            MaBHYTTNModel maBHYTTNModel = new MaBHYTTNModel();
            string json = JsonConvert.SerializeObject(maBHYTTNModel.SearchBHYTTNModel(maThe, hoTen, loaiBH, dotBh, mucBH), Formatting.Indented);
            return json;
        }

        public object SearchBHYTTNModelByDot(string dotBH)
        {
            MaBHYTTNModel maBHYTTNModel = new MaBHYTTNModel();
            string json = JsonConvert.SerializeObject(maBHYTTNModel.SearchBHYTTNModelByDot(dotBH), Formatting.Indented);
            return json;
        }

        public object SearchMaDotBHModel(string donVi, string tinh, string doiTuong, string loaiBH, string mucBH)
        {
            MaDotBHModel maDotBHModel = new MaDotBHModel();

            string json = JsonConvert.SerializeObject(maDotBHModel.SearchMaDotBHModel(donVi, tinh, doiTuong, loaiBH, mucBH), Formatting.Indented);
            return json;
        }


        public object GetCoSoKCBModel()
        {
            CoSoKCBModel coSoKCBModel = new CoSoKCBModel();
            string json = JsonConvert.SerializeObject(coSoKCBModel.GetCoSoKCBModel(), Formatting.Indented);
            return json;
        }

        public object GetDonViBHModel()
        {
            DonViBHModel donViBHModel = new DonViBHModel();

            string json = JsonConvert.SerializeObject(donViBHModel.GetDonViBHModel(), Formatting.Indented);
            return json;
        }

        public object GetHuyenModel()
        {
            HuyenModel huyenModel = new HuyenModel();

            string json = JsonConvert.SerializeObject(huyenModel.GetHuyenModel(), Formatting.Indented);
            return json;
        }

        public object GetHuyenByTinh(string maTinh)
        {
            HuyenModel huyenModel = new HuyenModel();

            string json = JsonConvert.SerializeObject(huyenModel.GetHuyenByTinh(maTinh), Formatting.Indented);
            return json;
        }

        public object GetLoaiBHModel()
        {
            LoaiBHModel loaiBHModel = new LoaiBHModel();

            object json = JsonConvert.SerializeObject(loaiBHModel.GetLoaiBHModel(), Formatting.Indented);
            return json;
        }

        public object GetMucBHModel()
        {
            MucBHModel mucBHModel = new MucBHModel();

            string json = JsonConvert.SerializeObject(mucBHModel.GetMucBHModel(), Formatting.Indented);
            return json;
        }

        public object GetMaDotBHModel()
        {
            MaDotBHModel maDotBHModel = new MaDotBHModel();

            string json = JsonConvert.SerializeObject(maDotBHModel.GetMaDotBHModel(), Formatting.Indented);
            return json;
        }

        public object GetTinhModel()
        {
            TinhModel tinhModel = new TinhModel();

            string json = JsonConvert.SerializeObject(tinhModel.GetTinhModel(), Formatting.Indented);
            return json;
        }

        public object GetNguoiSudungModel()
        {
            NguoiSudungModel nguoiSudungModel = new NguoiSudungModel();

            string json = JsonConvert.SerializeObject(nguoiSudungModel.GetNguoiSudungModel(), Formatting.Indented);
            return json;
        }


        public object insertBHYTTN(string MaDotBH, string HoVaTen, string NgaySinh, string GioiTinh, string DiaChi, string CoSoKCB, string TuNgay, string DenNgay, string LoaiBH, string MucBH, string NSD, string GhiChu)
        {

            MaBHYTTNModel maBHYTTNModel = new MaBHYTTNModel();
            if (maBHYTTNModel.Insert(MaDotBH, HoVaTen, NgaySinh, GioiTinh, DiaChi, CoSoKCB, TuNgay, DenNgay, LoaiBH, MucBH, NSD, GhiChu))
            {
                return "200";
            }
            else {
                return "400";
            }
        }

        public object insertDotBaoHiem(string DonViBH, string MaTinh, string Huyen, string DoiTuong, string NgayLap, string LoaiBH, string MucBH, string SoTien1Nam, string NguoiSudung)
        {

            MaDotBHModel maDotBHModel = new MaDotBHModel();
            if (maDotBHModel.Insert(DonViBH, MaTinh, Huyen, DoiTuong, NgayLap, LoaiBH, MucBH, SoTien1Nam, NguoiSudung))
            {
                return "200";
            }
            else
            {
                return "400";
            }
        }

        public object deleteBHYTTN(string id)
        {
            MaBHYTTNModel maBHYTTNModel = new MaBHYTTNModel();
            if (maBHYTTNModel.Delete(id))
            {
                return "200";
            }
            else
            {
                return "400";
            }
        }

        public object deleteDotBaoHiem(string id)
        {
            MaDotBHModel maDotBHModel = new MaDotBHModel();
            if (maDotBHModel.Delete(id))
            {
                return "200";
            }
            else
            {
                return "400";
            }
        }

        public object GetDotBHById(string id)
        {
            MaDotBHModel dotBHModel = new MaDotBHModel();

            string json = JsonConvert.SerializeObject(dotBHModel.GetDotBHById(id), Formatting.Indented);
            return json;
        }

        public object GetBHYTTNById(string id)
        {
            MaBHYTTNModel maBHYTTNModel = new MaBHYTTNModel();

            string json = JsonConvert.SerializeObject(maBHYTTNModel.GetMaBHYTTNModelById(id), Formatting.Indented);
            return json;
        }

        public object updateDotBaoHiem(string MaDotBH, string DonViBH, string MaTinh, string Huyen, string DoiTuong, string NgayLap,
            string LoaiBH, string MucBH, string SoTien1Nam, string NguoiSudung)
        {
            MaDotBHModel maDotBHModel = new MaDotBHModel();
            if (maDotBHModel.update(MaDotBH, DonViBH, MaTinh, Huyen, DoiTuong, NgayLap, LoaiBH, MucBH, SoTien1Nam, NguoiSudung))
            {
                return "200";
            }
            else
            {
                return "400";
            }
        }

        public object updateBHYTTN(string MaBHYTTN, string MaDotBH, string HoVaTen, string NgaySinh, string GioiTinh,
            string DiaChi, string CoSoKCB, string TuNgay, string DenNgay, string LoaiBH, string MucBH, string GhiChu, string NSD)
        {
            MaBHYTTNModel maBHYTTNModel = new MaBHYTTNModel();
            if (maBHYTTNModel.Update(MaBHYTTN, MaDotBH, HoVaTen, NgaySinh, GioiTinh, DiaChi, CoSoKCB, TuNgay, DenNgay, LoaiBH, MucBH, GhiChu, NSD))
            {
                return "200";
            }
            else
            {
                return "400";
            }
        }

        public object Authentication(string email, string pass)
        {
            NguoiSudungModel nguoiSudungModel = new NguoiSudungModel();
            if (nguoiSudungModel.Authentication(email, pass) != null)
            {
                string json = JsonConvert.SerializeObject(nguoiSudungModel.Authentication(email, pass), Formatting.Indented);
                return json;
            }
            return "false";
        }

        public void GETDATA() 
        {
            NguoiSudungModel nguoiSudungModel = new NguoiSudungModel();

            string json = JsonConvert.SerializeObject(nguoiSudungModel.GetNguoiSudungModel());
            Response.Write(json);          
        }
    }
}
